﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZBJF.Warranty.BLL;
using ZBJF.Warranty.Common.Enums.System;
using ZBJF.Warranty.Model;
using ZBJF.Warranty.Model.ViewModel.ApplicationCleaning;
using ZBJF.Warranty.WebProvider;

namespace ZBJF.Warranty.AdminSite.Areas.Cleaning.Controllers
{
    public class ServiceProductController : BaseController
    {

        #region 服务商产品页面 + ActionResult ServiceProductIndex()
        public ActionResult ServiceProductIndex()
        {
            return View();
        }
        #endregion

        #region 服务商产品管理页面获取列表数据 + ActionResult ServiceProductList(ServiceProductModel model)
        /// <summary>
        /// 服务商产品管理页面获取列表数据
        /// </summary>
        /// <returns></returns>
        public ActionResult ServiceProductList(ServiceProductModel model)
        {
            var total = 0;
            List<ServiceProductViewModel> list = ServiceFactory.ServiceProductServiceInstance.GetPagingList(model, out total);
            return Json(new
            {
                sEcho = model.sEcho,
                iTotalRecords = total,
                iTotalDisplayRecords = total,
                aaData = list
            }, JsonRequestBehavior.AllowGet);
        }
        #endregion

        #region 服务商产品编辑 + ActionResult ServiceProductEdit(int id)
        /// <summary>
        /// 服务商产品编辑
        /// </summary>
        /// <returns></returns>
        public ActionResult ServiceProductEdit(int id)
        {
            ViewBag.ShowTitle = id > 0 ? "编辑服务商产品" : "添加服务商产品";
            var ServiceProduct = ServiceFactory.ServiceProductServiceInstance.LoadByID(id);
            return View(ServiceProduct);
        }

        #endregion

        #region 保存服务商产品 + ActionResult SaveServiceProduct(ServiceProduct model)

        [HttpPost]
        [ValidateInput(false)]
        //保存服务商产品
        public ActionResult SaveServiceProduct(ServiceProduct model)
        {
            var responseResult = new ResponseResult
            {
                OpreateType = model.ID > 0 ? OpreateType.Update : OpreateType.Add
            };

            var serv = ServiceFactory.ServiceProductServiceInstance;
            model.ServiceProductName = model.ServiceProductName.Trim();
            if (model.ID == 0)
            {
                if (serv.IsExist(model.ServiceId, model.ServiceProductName))
                {
                    responseResult.Message = "该服务商产品已经存在";
                }
                else
                {
                    model.CreateTime = DateTime.Now;
                    model.CreateBy = CurrentUser.UserAccount;
                    model.LastModifyBy = CurrentUser.UserAccount;
                    model.LastModifyTime = DateTime.Now;
                    var entity = serv.AddEntities(model);
                    responseResult.Success = entity.ID > 0;
                }
            }
            else
            {
                var entity = serv.LoadByID(model.ID);
                if (entity != null)
                {
                    if (serv.IsExist( model.ServiceId, model.ServiceProductName, model.ID))
                    {
                        responseResult.Message = "已存在同名的服务商产品，请修改服务商产品名称";
                    }
                    //entity = ReflectHelper<ServiceProduct, ServiceProduct>.CloneT1ToT2(model, entity);
                    entity.ServiceId = model.ServiceId;
                    entity.ServiceProductPrice = model.ServiceProductPrice;
                    entity.ServiceProductName = model.ServiceProductName;
                    entity.ServiceProductId = model.ServiceProductId;
                    entity.LocalProductId = model.LocalProductId;
                    entity.IsAvailable = model.IsAvailable;
                    entity.LastModifyBy = CurrentUser.UserAccount;
                    entity.LastModifyTime = DateTime.Now;
                    responseResult.Success = ServiceFactory.ServiceProductServiceInstance.UpdateEntities(entity);
                }
                else
                {
                    responseResult.Message = "该服务商产品不存在或已被删除";
                }
            }
            TempData["ResponseResult"] = responseResult;
            return responseResult.Success ? RedirectToAction("ServiceProductIndex") : RedirectToAction("ServiceProductEdit", new { id = model.ID });
        }
        #endregion

        #region 禁用/启用服务商产品 + JsonResult UpdateStatus(int id, bool isAvailable)
        /// <summary>
        /// 禁用/启用服务商产品
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <param name="isAvailable">启用/禁用</param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult UpdateStatus(int id, bool isAvailable)
        {
            var entity = ServiceFactory.ServiceProductServiceInstance.LoadByID(id);
            var success = false;
            if (entity != null)
            {
                entity.IsAvailable = isAvailable;
                entity.LastModifyBy = CurrentUser.UserAccount;
                entity.LastModifyTime = DateTime.Now;
                success = ServiceFactory.ServiceProductServiceInstance.UpdateEntities(entity);
            }
            return Json(new
            {
                success = success,
                message = success ? "修改成功" : "修改失败"
            });
        }
        #endregion
    }
}